#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "../global.h"
#include "../tools/str.h"
#include "set_config_data.h"


//设置配置结构体函数
void SetServiceIp(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char *service_ip = va_arg(ap,char*);
    StrCopy(data->ip,service_ip);
    va_end(ap);
}

void SetPort(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char* port = va_arg(ap,char*);
    int num;
    StrToNum(port,(int *)&data->port);
    va_end(ap);
}

void SetHtmlPath(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char *html_path = va_arg(ap,char*);
    StrCopy(data->html_path,html_path);
    va_end(ap);
}

void SetHtmlFileName(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char *html_file_name = va_arg(ap,char*);
    StrCopy(data->html_file_name,html_file_name);
    va_end(ap);
}

void Set404HtmlFileName(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char *html_file_404 = va_arg(ap,char*);
    StrCopy(data->html_file_404,html_file_404);
    va_end(ap);
}


//设置CGI配置文件
void SetCGIStatus(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char *tmp = va_arg(ap,char*);

    if (StrCmp(tmp,"yes")==EQUAL)
    {
        data->is_use_cgi = YES;
    }else{
        data->is_use_cgi = NO;
    }
    
    va_end(ap);
}

const char CGIFileClass[CGI_FILE_CLASS_NUM][8]={
    ".py",".cgi"
};

void SetCGIFileClass(CONFIG_DATA *data,int n,...){
    va_list ap;
    va_start(ap,n);
    char *tmp = va_arg(ap,char*);
    char *tokstr=NULL;
    data->cgi_file_class.value=0;
    tokstr = strtok(tmp," ");
    for (size_t i = 0;tokstr != NULL; i++)
    {
        
       if (StrCmp(tokstr,CGIFileClass[i])==EQUAL)
       {
           data->cgi_file_class.value |= (1<<i);
       }
       tokstr = strtok(NULL," ");
  
    }
    va_end(ap);
}


const TABLE_DATA ConfigTable[CONFIG_TABLE_NUM]={
    {"service_ip",SetServiceIp},
    {"port",SetPort},
    {"html_path",SetHtmlPath},
    {"html_name",SetHtmlFileName},
    {"html_404",Set404HtmlFileName},
    {"cgi_status",SetCGIStatus},
    {"cgi_file_class",SetCGIFileClass}
};
